<!-- eslint-disable -->
<template>
  <el-card class="box-card">
    <!-- 模糊查询 -->
    <el-form :inline="true" :model="searchForm" class="demo-form-inline">
      <el-form-item label="用户名">
        <el-input v-model="searchForm.username" placeholder="用户名"></el-input>
      </el-form-item>
      <el-form-item label="姓名">
        <el-input v-model="searchForm.nickname" placeholder="姓名"></el-input>
      </el-form-item>
      <el-form-item label="电话">
        <el-input v-model="searchForm.phone" placeholder="电话"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="search">查询</el-button>
      </el-form-item>
    </el-form>
    <el-button
      type="primary"
      icon="el-icon-plus"
      style="margin-bottom: 50px; margin-top: 50px"
      @click="dialogVisible = true"
      :disabled="role == 'user'"
      >添加学员</el-button
    >

    <el-table :data="userList" border style="width: 100%">
      <el-table-column prop="id" label="UUID" width="100" align="center" ></el-table-column>
      <el-table-column
      align="center"
        prop="username"
        label="用户名"
        width="100"
      ></el-table-column>
      <el-table-column
      align="center"
        prop="nickname"
        label="姓名"
        width="100"
      ></el-table-column>
      <el-table-column prop="age" label="年龄" width="80" align="center"></el-table-column>
      <el-table-column prop="gender" label="性别" width="80" align="center"></el-table-column>
      <el-table-column prop="photo" label="头像" width="120" class="imgwrap" align="center">
        <template v-slot="{ row }">
          <img :src="row.photo" alt />
        </template>
      </el-table-column>
      <el-table-column prop="phone" label="电话" width="150" align="center"></el-table-column>
      <el-table-column
      align="center"
        prop="date"
        label="报名时间"
        width="200"
      ></el-table-column>
      <el-table-column prop="address" label="操作" align="center">
        <template slot-scope="{ row, index }">
          <el-button
            type="danger"
            size="medium"
            icon="el-icon-delete"
            title="删除学员"
            @click="deleteStudent(row)"
            :disabled="role !== 'admin'"
          ></el-button>
          <el-button
            type="warning"
            size="medium"
            icon="el-icon-edit"
            title="修改学员信息"
            @click="toUpdate(row)"
            :disabled="role !== 'admin'"
          ></el-button>
          <el-button
            type="info"
            size="medium"
            icon="el-icon-message"
            title="查看学员详细信息"
            @click="getUserInfo(row)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :page-sizes="[4, 6, 8, 10]"
      :page-size="100"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    ></el-pagination>
    <!-- 添加的弹出框 -->
    <el-dialog title="添加学员" :visible.sync="dialogVisible" width="30%">
      <!-- 这里写添加用户的表单 -->
      <el-form :model="form">
        <el-form-item label="用户名">
          <el-input
            v-model="form.username"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item label="姓名">
          <el-input v-model="form.nickname" placeholder="请输入姓名"></el-input>
        </el-form-item>
        <el-form-item label="年龄">
          <el-input v-model="form.age" placeholder="请输入姓名"></el-input>
        </el-form-item>
        <el-form-item label="性别">
          <el-input v-model="form.gender" placeholder="请输入姓名"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input v-model="form.password" placeholder="请输入姓名"></el-input>
        </el-form-item>
        <el-form-item label="电话">
          <el-input
            v-model="form.phone"
            placeholder="请输入电话号码"
          ></el-input>
        </el-form-item>
        <el-form-item label="日期" :formatter="formatData">
          <el-date-picker
            type="date"
            placeholder="选择日期"
            v-model="form.date"
          ></el-date-picker>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addStudent">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 修改弹出层 -->
    <el-dialog title="修改学员信息" :visible.sync="updateStatus" width="30%">
      <!-- 这里写添加用户的表单 -->
      <el-form :model="form">
        <el-form-item label="用户名" prop="username">
          <el-input
            v-model="form.username"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item label="姓名" prop="nickname">
          <el-input v-model="form.nickname" placeholder="请输入姓名"></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="nickname">
          <el-input v-model="form.gender" placeholder="请输入姓名"></el-input>
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input v-model="form.age" placeholder="请输入姓名"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input
            v-model="form.phone"
            placeholder="请输入电话号码"
          ></el-input>
        </el-form-item>
        <el-form-item label="日期" prop="date">
          <el-date-picker
            type="date"
            placeholder="选择日期"
            v-model="form.date"
          ></el-date-picker>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="updateStatus = false">取 消</el-button>
        <el-button type="primary" @click="updateStudent">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 学院员详情 -->
    <el-dialog title="学员详情" :visible.sync="infoStatus" width="50%">
      <el-form :model="info">
        <el-form-item style="width: 600px" label="头像">
          <el-upload
            class="avatar-uploader"
            action="https://jsonplaceholder.typicode.com/posts/"
            :show-file-list="false"
          >
            <img v-if="info.photo" :src="info.photo" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="用户名">
          <el-input
            v-model="info.username"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item label="姓名">
          <el-input v-model="info.nickname" placeholder="请输入姓名"></el-input>
        </el-form-item>
        <el-form-item label="性别">
          <el-input v-model="info.gender" placeholder="请输入姓名"></el-input>
        </el-form-item>
        <el-form-item label="年龄">
          <el-input v-model="info.age" placeholder="请输入姓名"></el-input>
        </el-form-item>
        <el-form-item label="电话">
          <el-input
            v-model="info.phone"
            placeholder="请输入电话号码"
          ></el-input>
        </el-form-item>
        <el-form-item label="日期">
          <el-date-picker
            type="info"
            placeholder="选择日期"
            v-model="info.date"
          ></el-date-picker>
        </el-form-item>
      </el-form>
    </el-dialog>
  </el-card>
</template>

<script>
/* eslint-disable */
import {
  //
  userList,
  addStudent,
  deleteStu,
  updateStu,
  getInfo,
  searchStudent,
} from "@/api/student";
import { mapState } from "vuex";
import { getToken } from "@/utils/auth";
export default {
  name: "Student",
  data() {
    return {
      token:getToken(),
      searchForm: {
        nickname: null,
        username: null,
        phone: null,
      },
      infoStatus: false,
      pageNumber: 1, //当前页
      pagesize: 4, //分页的数据
      total: 0, //总页数
      userList: [],
      updateStatus: false,
      dialogVisible: false,
      form: {
        date: "",
        phone: "",
        nickname: "",
        password: "",
        username: "",
        id: "",
        age: "",
        gender: "",
      },
      row: {},
      info: {},
    };
  },
  methods: {
    formatData(row, column, cellValue, index){
		

		var s =	new Date(cellValue).toLocaleString();
	    return s;

	    },

    handleSizeChange(val) {
      this.pagesize = val;
      this.getUserList();
    },
    handleCurrentChange(val) {
      this.pageNumber = val;
      this.getUserList();
    },
    // 分页查询
    async getUserList() {
      let res = await userList({
        pageNumber: this.pageNumber,
        pagesize: this.pagesize,
      });
      if (res.code == 200) {
        this.userList = res.data;
        this.userList.forEach(item => {
            item.date = this.dayjs(item.date).format("YYYY-MM-DD HH:mm:ss")
            item.photo = item.photo + `?token=${this.token}`
        });
        this.total = res.totalCount;
      }
    },
    // 添加用户
    async addStudent() {
      let res = await addStudent(this.form);

      if (res.code == 200) {
        this.$message({
          message: res.message,
          type: "success",
        });
        this.getUserList();
      }
      this.dialogVisible = false;
      this.form = {};
    },
    // 删除用户的按钮
    async deleteStudent(row) {
      let res = await deleteStu(row.id);
      if (res.code == 200) {
        this.getUserList();
        this.$message({
          message: res.message,
          type: "success",
        });
      }
    },
    // 前往修改信息界面
    toUpdate(row) {
      // 当前用户信息
      this.form.id = row.id;
      this.form.username = row.username;
      this.form.nickname = row.nickname;
      this.form.date = row.date;
      this.form.password = row.password;
      this.form.phone = row.phone;
      this.form.gender = row.gender;
      this.form.age = row.age;
      this.updateStatus = true;
    },
    // 点击确定修改信息
    async updateStudent() {
      let res = await updateStu(this.form);
      console.log(res, "修改信息");
      this.updateStatus = false;
      if (res.code == 200) {
        this.getUserList();
        this.$message({
          message: res.message,
          type: "success",
        });
      }
    },
    async getUserInfo(row) {
      this.infoStatus = true;
      let res = await getInfo(row.id);
      if (res.code == 200) {
        this.info = res.data;
      }
      console.log(res);
    },
    async search() {
      if (this.searchForm.username == "") this.searchForm.username = null;
      if (this.searchForm.nickname == "") this.searchForm.nickname = null;
      if (this.searchForm.phone == "") this.searchForm.phone = null;
      if (this.searchForm.username == null && this.searchForm.nickname==null&&this.searchForm.phone==null){
        this.getUserList()
        return
      }
     
      let res = await searchStudent(this.searchForm);
      if (res.code == 200) {
        this.$message({
          message: res.message,
          type: "success",
        });
        this.userList = res.data;
        this.total = res.data.length;
      }
     
     
    },
  },
  mounted() {
    this.getUserList();
  },
  computed: {
    ...mapState({
      role: (store) => store.user.info.role,
      info: (store) => store.user.info,
    }),
  },
};
</script>

<style scoped lang="scss">
img {
  width: 100px;
  height: 100px;
  margin-right: 15px;
}
.box-card {
  width: 1200px;
  margin-left: 50px;
  margin-top: 50px;
}
</style>
